﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNA.Mvc.Data.Entity
{
    public class FriendRepository : EntityRepository<Friend>, IFriendRepository
    {
        public FriendRepository() : base() { }

        public FriendRepository(DB dbContext) : base(dbContext) { }

        public virtual Friend Find(string userName, string friendName)
        {
            return dbSet.FirstOrDefault(f => f.UserName.Equals(userName) && f.FriendName.Equals(friendName));
        }

        public virtual IQueryable<Friend> GetMyFriends(string userName)
        {
            return dbSet.Where(f => f.UserName.Equals(userName) && f.IsFriend).OrderByDescending(f => f.Impression);
        }

        public virtual IQueryable<Friend> GetMyFriends(string userName, int pageIndex, int pageSize, out int totalRecords)
        {
            var skips = pageIndex * pageSize;
            totalRecords = dbSet.Count(f => f.UserName.Equals(userName) && f.IsFriend);
            var query = dbSet.Where(f => f.UserName.Equals(userName) && f.IsFriend).OrderByDescending(f => f.Impression);
            if (skips > 0)
                return query.Skip(skips).Take(pageSize);
            else
                return query.Take(pageSize);
        }

        public virtual IQueryable<string> GetMyFollowers(string userName)
        {
            return Context.Followers.Where(f => f.UserName.Equals(userName)).Select(f => f.FollowerName);
        }

        public virtual IQueryable<string> GetMyFollowers(string userName, int pageIndex, int pageSize, out int totalRecords)
        {
            var skips = pageIndex * pageSize;
            totalRecords = Context.Followers.Count(f => f.UserName.Equals(userName));
            var query = Context.Followers.Where(f => f.UserName.Equals(userName)).Select(f => f.FollowerName);
            if (skips > 0)
                return query.Skip(skips).Take(pageSize);
            else
                return query.Take(pageSize);
        }

        public virtual IQueryable<string> GetMyFollows(string userName)
        {
            return Context.Followers.Where(f => f.FollowerName.Equals(userName)).Select(f => f.UserName);
        }

        public virtual IQueryable<string> GetMyFollows(string userName, int pageIndex, int pageSize, out int totalRecords)
        {
            var skips = pageIndex * pageSize;
            totalRecords = Context.Followers.Count(f => f.FollowerName.Equals(userName));
            var query = Context.Followers.Where(f => f.FollowerName.Equals(userName)).Select(f => f.UserName);
            if (skips > 0)
                return query.Skip(skips).Take(pageSize);
            else
                return query.Take(pageSize);
        }

        public virtual IQueryable<Friend> GetBadGuys(string userName)
        {
            return dbSet.Where(f => f.UserName.Equals(userName) && f.IsBadGuy).OrderByDescending(f => f.Impression);
        }

        public virtual IQueryable<Friend> GetFriendsWatingFor(string userName)
        {
            return dbSet.Where(f => f.UserName.Equals(userName) && !f.IsFriend && !f.IsBadGuy).OrderByDescending(f => f.Impression);
        }

        public virtual IQueryable<Friend> GetFriendsWatingFor(string userName, int pageIndex, int pageSize, out int totalRecords)
        {
            var skips = pageIndex * pageSize;
            totalRecords = dbSet.Count(f => f.UserName.Equals(userName) && !f.IsFriend && !f.IsBadGuy);
            var query = dbSet.Where(f => f.UserName.Equals(userName) && !f.IsFriend && !f.IsBadGuy).OrderByDescending(f => f.Impression);
            if (skips > 0)
                return query.Skip(skips).Take(pageSize);
            else
                return query.Take(pageSize);
        }
    }
}
